
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       sitl_state.h
  * @author     baiyang
  * @date       2022-12-1
  ******************************************************************************
  */

#pragma once

#ifdef __cplusplus
extern "C"{
#endif

/*----------------------------------include-----------------------------------*/
#include "sitl.h"
#include "sim_aircraft.h"

#include <stdint.h>
#include <stdbool.h>

#include <common/utility/mb_socket.h>
/*-----------------------------------macro------------------------------------*/
#ifndef SITL_RC_INPUT_CHANNELS
#define SITL_RC_INPUT_CHANNELS 16
#endif
/*----------------------------------typedef-----------------------------------*/
enum vehicle_type {
    SIM_Copter,
    SIM_Rover,
    SIM_Plane,
    SIM_Sub,
    SIM_Blimp
};

typedef struct sitl_state* sitl_state_t;
typedef const struct sitl_state* sitl_state_const_t;

struct sitl_state {
    bool construction_completed;

    uint16_t pwm_output[SITL_NUM_CHANNELS];
    uint16_t pwm_input[SITL_RC_INPUT_CHANNELS];
    bool output_ready;                             // false
    bool new_rc_input;

    // simulated airspeed, sonar and battery monitor
    uint16_t sonar_pin_value;    // pin 0
    uint16_t airspeed_pin_value[AIRSPEED_MAX_SENSORS]; // pin 1
    uint16_t voltage_pin_value;  // pin 13
    uint16_t current_pin_value;  // pin 12
    uint16_t voltage2_pin_value;  // pin 15
    uint16_t current2_pin_value;  // pin 14

    // paths for UART devices
    const char *_uart_path[9];

    // internal state
    enum vehicle_type _vehicle;
    uint8_t _instance;
    uint16_t _base_port;
    uint16_t _udp_port_offset;
    pid_t _parent_pid;
    uint32_t _update_count;

    struct mb_socket _sitl_rc_in;  // degault, true
    sim_t    _sitl;
    uint16_t _rcin_port;
    uint16_t _fg_view_port;
    uint16_t _irlock_port;
    float _current;

    bool _synthetic_clock_mode;

    bool _use_rtscts;
    bool _use_fg_view;
    
    const char *_fg_address;

    uint32_t wind_start_delay_micros;

    // internal SITL model
    sim_aircraft_t sitl_model;

    // output socket for flightgear viewing
    struct mb_socket fg_socket;  // degault, true

    const char *defaults_path;

    char *_gps_fifo[2];
};
/*----------------------------------variable----------------------------------*/

/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
sitl_state_t sitlstate_get_singleton();
void sitlstate_ctor(sitl_state_t sitl);
void sitlstate_init(sitl_state_t sitl, int argc, char * const argv[]);
void sitlstate_sitl_setup(sitl_state_t sitl);
void sitlstate_setup_fdm(sitl_state_t sitl);
void sitlstate_fdm_input_step(sitl_state_t sitl);

bool sitlstate_parse_command_line(sitl_state_t sitl, int argc, char * const argv[]);
/*------------------------------------test------------------------------------*/

#ifdef __cplusplus
}
#endif



